﻿

using System;
using AutoMapper;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;

using Oliving.Edge.AuthHelper;
using Oliving.Edge.Model;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IServices;
using Oliving.Edge.IServices.DTSN;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.Model.Models.DTSN;
using Oliving.Edge.Model.Dto.DTSN;
using System.Linq.Expressions;
using Oliving.Edge.Common.Extensions;

namespace Oliving.Edge.Controllers
{
    [ApiController]
    [Route("api/DTSN_WuLiaoType")]
    [Authorize(Permissions.Name)]
    public class DTSN_WuLiaoTypeController : OlivingControll
    {
        IMapper _mapper;
        private readonly IDTSN_WuLiaoTypeServices _DTSN_WuLiaoTypeServices;
        private readonly ILogger<DTSN_WuLiaoTypeController> _logger;


        public DTSN_WuLiaoTypeController(
                IHttpContextAccessor accessor, PermissionRequirement requirement,
                IMapper mapper, ILogger<DTSN_WuLiaoTypeController> logger,
                IDTSN_WuLiaoTypeServices DTSN_WuLiaoTypeServices
            ) : base(accessor, requirement)
        {
            _logger = logger;
            _mapper = mapper;
            _DTSN_WuLiaoTypeServices = DTSN_WuLiaoTypeServices;
        }



        [HttpGet]
        [AllowAnonymous]
        [Route("/api/[controller]/Get")]
        public async Task<MessageModel<DTSN_WuLiaoTypeDto>> Get(int id)
        {
            var data = new MessageModel<DTSN_WuLiaoTypeDto>();
            DTSN_WuLiaoType model = await _DTSN_WuLiaoTypeServices.QueryById(id);
            if (model == null)
            {
                data.response = null;
                data.msg = "数据不存在！";
                data.success = false;
            }
            else
            {
                var dto = _mapper.Map<DTSN_WuLiaoTypeDto>(model);
                data.response = dto;
                data.msg = "获取成功！";
                data.success = true;
            }
            return data;
        }




        [HttpGet]
        [Route("/api/[controller]/GetPageList")]
        public async Task<MessageModel<PageModel<DTSN_WuLiaoTypePageDto>>> GetPageList(
            int page = 1, int PageSize = 20, string sidx = "", string order = "", string key = "")
        {
            Expression<Func<DTSN_WuLiaoType, bool>> whereExpression = _DTSN_WuLiaoType => true;
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
            else
            {
                whereExpression = whereExpression.And(d =>
                    d.WT_FullName.Contains(key) ||
                    d.WT_Name.Contains(key) ||
                    d.WT_Code.Contains(key)
                );
            }

            var data = await _DTSN_WuLiaoTypeServices.QueryPageList(whereExpression, page, PageSize, sidx, order);

            return new MessageModel<PageModel<DTSN_WuLiaoTypePageDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }



        [HttpPut]
        [Route("/api/[controller]/Update")]
        public async Task<MessageModel<string>> Update([FromBody] DTSN_WuLiaoTypeDto dto)
        {
            var data = new MessageModel<string>();

            if (dto != null && dto.WT_ID > 0)
            {
                DTSN_WuLiaoType model = await _DTSN_WuLiaoTypeServices.QueryById(dto.WT_ID);
                if (model != null)
                {
                    model.WT_Code = dto.WT_Code;
                    model.WT_FullName = dto.WT_FullName;
                    model.WT_Name = dto.WT_Name;
                    model.WT_CreateTime = dto.WT_CreateTime;
                    model.WT_PID = dto.WT_PID;


                    data.success = await _DTSN_WuLiaoTypeServices.Update(model);

                    if (data.success)
                    {
                        data.msg = "更新成功";
                        data.response = dto.WT_ID.ObjToString();
                    }
                }
                else
                {
                    data.success = false;
                    data.msg = "参数异常，您要更新的数据不存在";
                }
            }
            else
            {
                data.success = false;
                data.msg = "参数异常";
            }
            return data;
        }





        [HttpPost]
        [Route("/api/[controller]/Add")]
        public async Task<MessageModel<string>> Add(DTSN_WuLiaoType dto)
        {
            var data = new MessageModel<string>();
            int id = await _DTSN_WuLiaoTypeServices.Add(dto);

            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }
            return data;
        }




        [HttpDelete]
        [Route("/api/[controller]/Delete")]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            if (id > 0)
            {
                var userDetail = await _DTSN_WuLiaoTypeServices.QueryById(id);
                // userDetail.C_State = -1; ?????????????
                data.success = await _DTSN_WuLiaoTypeServices.Update(userDetail);
                if (data.success)
                {
                    data.msg = "删除成功";
                    data.response = userDetail?.WT_ID.ObjToString();
                }
            }
            return data;
        }





        [HttpDelete]
        [Route("/api/[controller]/BatchDelete")]
        public async Task<MessageModel<string>> BatchDelete(string ids)
        {
            var data = new MessageModel<string>();
            if (!string.IsNullOrEmpty(ids))
            {
                if (false)
                {
                    #region 软删除

                    int[] list = Array.ConvertAll<string, int>(ids.Split(','), s => int.Parse(s));

                    DTSN_WuLiaoType model = new DTSN_WuLiaoType();
                    //  model.C_State = -1;          ???????????????????
                    string whereStr = " WT_ID in (" + ids + ")";
                    List<string> colList = new List<string>();
                    //  colList.Add("C_State");    ???????????????????
                    List<string> colHL = new List<string>();

                    bool isOK = await _DTSN_WuLiaoTypeServices.Update(model, colList, colHL, whereStr);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }

                    #endregion
                }
                {
                    #region 直接删除 

                    object[] list = Array.ConvertAll<string, object>(ids.Split(','), s => int.Parse(s));

                    bool isOK = await _DTSN_WuLiaoTypeServices.DeleteByIds(list);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }
                    #endregion
                }
            }
            return data;
        }

         

        [HttpGet]
        [Route("/api/[controller]/GetList")]
        [AllowAnonymous]
        public async Task<MessageModel<List<DTSN_WuLiaoType>>> GetList(string key = "")
        {
            Expression<Func<DTSN_WuLiaoType, bool>> whereExpression = _DTSN_WuLiaoType => true;
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
            else
            {
                whereExpression = whereExpression.And(d =>
                    d.WT_FullName.Contains(key) ||
                    d.WT_Name.Contains(key) ||
                    d.WT_Code.Contains(key)
                );
            }

            List<DTSN_WuLiaoType> outList = await _DTSN_WuLiaoTypeServices.Query(whereExpression, " WT_Code asc  ");
             
            return new MessageModel<List<DTSN_WuLiaoType>>()
            {
                msg = "获取成功",
                success = true,
                response = outList
            };
        }






    }
}

